package com.example.server.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.server.entity.Course;
import com.example.server.entity.Dingdan;
import com.example.server.entity.Stage;
import com.example.server.service.CourseService;
import com.example.server.service.DingdanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (Course)表控制层
 *
 * @author makejava
 * @since 2022-11-21 09:10:57
 */
@RestController
@RequestMapping("course")
public class CourseController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;
    @Resource
    private DingdanService dingdanService;
    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     * 不需要参数列表，返回所有课程信息
     */
    @GetMapping
    public R selectAll() {
        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
//        queryWrapper.select("s");
        List<Course> re = courseService.getBaseMapper().selectList(null);
        if(re.size()>0)
        {
            r.setCode(200);
            r.setData(re);
            r.setMsg("查询成功");
            return r;
        }
        r.setCode(404);
        r.setData(re);
        r.setMsg("查询失败");
        return r;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {

        R r =new R<>();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<Stage> re = courseService.getBaseMapper().selectList(queryWrapper);
        if(re.size()>0)
        {
            r.setCode(200);
            r.setData(re);
            r.setMsg("查询成功");
            return r;
        }
        r.setCode(404);
        r.setData(re);
        r.setMsg("查询失败");
        return r;
    };
    /**
     * 通过用户id查询用户所有支付完成的课程（关联订单表）
     */
    @GetMapping("/user/{id}")
    public R selectuser(@PathVariable Serializable id) {

        R r =new R<>();
        QueryWrapper queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("userid",id);
        queryWrapper1.eq("audit",1);
        List<Dingdan> re = dingdanService.getBaseMapper().selectList(queryWrapper1);//查询所有用户支付成功的订单
        List<Course> rs = new ArrayList<Course>(); ;
        if(re.size()>0)
        {
            for(int i=0;i<re.size();i++){
                QueryWrapper queryWrapper2 =new QueryWrapper<>();
                queryWrapper2.eq("id", re.get(i).getCourseid());
                  rs.add(courseService.getBaseMapper().selectOne(queryWrapper2));//查询所有用户支付成功的订单
            }
            r.setCode(200);
            r.setData(rs);
            r.setMsg("查询成功");
            return r;
        }
        r.setCode(404);
        r.setData(re.size());
        r.setMsg("查询失败");
        return r;
    }

    /**
     * 新增数据
     *
     * @param course 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Course course) {
        return success(this.courseService.save(course));
    }

    /**
     * 修改数据
     *
     * @param course 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Course course) {
        return success(this.courseService.updateById(course));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.courseService.removeByIds(idList));
    }
}

